package student

import (
	"fmt"
	"os"
	"strconv"
	"time"
)

func Object() {
	var zhangsan person
	zhangsan.name = "张三"
	zhangsan.age = 18
	zhangsan.address = "重庆万州"
	zhangsan.money = 198
	zhangsan.height = 1.80
	zhangsan.weight = 65
	zhangsan.buy("《刺客信条：幻景》")
	zhangsan.eat("老八秘制小汉堡")

	var lisi person
	lisi.name = "李四"
	lisi.age = 18
	lisi.address = "重庆九龙坡"
	lisi.money = 648
	lisi.height = 1.81
	lisi.weight = 71
	lisi.login()
	lisi.dayouxi()

	var laoba person
	laoba.name = "老八"
	laoba.age = 18
	laoba.address = "重庆江北"
	laoba.money = 1000
	laoba.height = 1.73
	laoba.weight = 60

	var edu float32

	fmt.Println(laoba.name, "现在的钱是：", laoba.money)
	fmt.Println(zhangsan.name, "现在的钱是：", zhangsan.money)
	laoba.zhuanzhang(&zhangsan, 25)
	fmt.Println(laoba.name, "转账之后的钱是：", laoba.money)
	fmt.Println(zhangsan.name, "转账之后钱是：", zhangsan.money)

	for i := 0; i < 5; i++ {
		fmt.Println(laoba.name, "现在的钱是：", laoba.money)
		fmt.Println(zhangsan.name, "现在的钱是：", zhangsan.money)
		fmt.Println(lisi.name, "现在的钱是：", lisi.money)
		fmt.Println("请输入发红包的额度：")
		fmt.Scanln(&edu)
		laoba.fahongbao(edu, &zhangsan, &lisi)
		fmt.Println(laoba.name, "发红包之后的钱是：", laoba.money)
		fmt.Println(zhangsan.name, "收红包之后钱是：", zhangsan.money)
		fmt.Println(lisi.name, "收红包之后钱是：", lisi.money)
	}

}

type person struct {
	//名字
	name string
	//年龄
	age int
	//家庭住址
	address string
	//钱财
	money float32
	//身高
	height float32
	//体重
	weight float32
}

func (p *person) buy(thing string) {
	fmt.Println(p.name, "在网购，买的是：", thing)

}

func (p *person) dayouxi() {
	fmt.Println(p.name, "在玩原神，年龄是：", p.age, "钱财有：", p.money, "身高为：", p.height, "体重为：", p.weight, "家庭住址是：", p.address)

}

func (p *person) login() {
	fmt.Println(p.name, "在登录王者荣耀，年龄是：", p.age, "钱财有：", p.money, "身高为：", p.height, "体重为：", p.weight, "家庭住址是：", p.address)

}

func (p *person) eat(food string) {
	fmt.Println(p.name, "在吃饭，吃的是：", food)
}

func (p *person) zhuanzhang(to *person, edu float32) bool {

	p.money = p.money - edu
	to.money = to.money + edu
	return true

}

func (p *person) fahongbao(edu float32, mubiao ...*person) {
	p.money = p.money - edu*float32(len(mubiao))
	for i := 0; i < len(mubiao); i++ {
		mubiao[i].money = mubiao[i].money + edu
	}

}

func SaveStudentRecords() {
	var allStudents []classmates.Classmate
	classmates.DB.Raw("SELECT*FROM classmates.classmates").Scan(&allStudents)
	for _, stu := range allStudents {
		mulu := "D:/go语言/" + strconv.Itoa(stu.ClassId)
		cuowu := os.MkdirAll(mulu, os.ModeDir)
		if cuowu != nil {
			fmt.Println("创建目录的时候出错，错误信息是：", cuowu)
			return
		}
		fmt.Println("创建的目录是：", mulu+"/"+stu.Sn+stu.Name+".txt")
		currentFile, err := os.Create(mulu + stu.Sn + stu.Name + ".txt")
		if err != nil {
			fmt.Println("创建失败的原因是：", err)
			return
		}
		fmt.Println("正在处理" + stu.Name + "的数据，请耐心等待...\n")
		var thisStudentRecord []classmates.Records
		classmates.DB.Raw("select*from classmates.records where sn=?", stu.Sn).Scan(&thisStudentRecord)
		currentFile.WriteString("面向对象程序设计的考勤记录\n")
		for _, records := range thisStudentRecord {
			currentFile.WriteString(records.Name + records.Createtime.String() + records.Comments + "\n")
		}
		currentFile.WriteString("以上就是该同学的所有考勤记录。等级时间是：" + time.Now().String())
		currentFile.Close()
	}

}
